jQuery(function() {
	jQuery("#isSuitably").val(jQuery("#isSuitably1").val());
	jQuery("#isArrive").val(jQuery("#isArrive1").val());
	var date = new Date();
	if(jQuery("#sendDate1").val() != ""){
		date =jQuery("#sendDate1").val();
	}
	var date = new Date();
	if(jQuery("#setDate1").val() != ""){
		date =jQuery("#setDate1").val();
	}
	var date1 = new Date();
	if(jQuery("#serviceDate1").val() != ""){
		date1 =jQuery("#serviceDate1").val();
	}
	jQuery("#sendDate").val(moment(date).format("YYYY-MM-DD"));
	jQuery("#setDate").val(moment(date).format("YYYY-MM-DD"));
	jQuery("#setDate2").val(moment(date).format("YYYY-MM-DD"));
	jQuery("#serviceDate").val(moment(date1).format("YYYY-MM-DD"));
	if(jQuery("#deliveryDate1").val() != ""){
		jQuery("#deliveryDate").val(moment(jQuery("#deliveryDate1").val()).format("YYYY-MM-DD"));
	}
	if(jQuery("#sendDateThree1").val() != ""){
		jQuery("#sendDateThree").val(moment(jQuery("#sendDateThree1").val()).format("YYYY-MM-DD"));
	}
	if(jQuery("#dealDate1").val() != ""){
		jQuery("#dealDate").val(moment(jQuery("#dealDate1").val()).format("YYYY-MM-DD"));
	}
	jQuery("#qualityGuarantee").val($("#qualityGuarantee1").val());
	var dutyBlongThree = jQuery("#dutyBlongThree1").val();
	var outsourcing = jQuery("#outsourcing1").val();
	var selfMake = jQuery("#selfMake1").val();
	$(":radio[name='dutyBlongThree'][value='" + dutyBlongThree + "']").prop("checked", "checked");
	$(":radio[name='outsourcing'][value='" + outsourcing + "']").prop("checked", "checked");
	$(":radio[name='selfMake'][value='" +selfMake+ "']").prop("checked", "checked");
	jQuery(".saveBtn").on('click', function() {
		if (jQuery("#tab1Form") && !jQuery("#tab1Form").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {return;}
		var ticketId = jQuery("#ticketId").val();
		var type = jQuery("#afterSaleType").val();
		var d;
		if (type!="三包办理"){
			d = {
					"isSuitably" : jQuery("#isSuitably").val(),
					"manageCode" : jQuery("#manageCode").val(),
					"questionTrance" : jQuery("#questionTrance").val(),
					"dealPlan" : jQuery("#dealPlan").val()
				}
		}
		else{
			d = {
					"manageCode" : jQuery("#manageCode").val(),
					"dealPlan" : jQuery("#dealPlan").val(),
					"questionTrance" : jQuery("#questionTrance").val(),
					"operator" : jQuery("#operator").val(),
					"dealRate" : jQuery("#dealRate").val(),
					"sendDate" : jQuery("#sendDate").val(),
					"isArrive" : jQuery("#isArrive").val(),
					"waybillNum" : jQuery("#waybillNum").val()
				}
		}	
		var url = window.rootPath + '/api/afterSaleTickets/' + ticketId;
		jQuery.ajax({
			url : encodeURI(url),
			type : 'patch',
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function() {
				var operateType = "售后服务单办理";
				var content = "已完善服务单【"+jQuery("#unitName").val()+"】";
				addOperateLog(operateType,content);
				$.springLemonDialog({
	   		         content: '服务单基本信息已保存！',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
			}

		});
	});
	//tab2保存按钮
	jQuery('.saveTab2Btn').on("click", function() {
		var tab2Id = $("#tab2Id").val();
		var d= 
		{
				"recordCode":$("#recordCode").val(),
				"deviceState":$("#deviceState").val(),
				"isGuarantee":$("#qualityGuarantee").val(),
				"dutyBlong":$("#dutyBlong").val(),
				"questionDescription":$("#questionDescription").val(),
				"reasonAnalyse":$("#reasonAnalyse").val(),
				"solvePlan":$("#solvePlan").val(),
				"memo":$("#memo").val()
		}
		var url = window.rootPath + '/api/afterSaleTicketBacks/' + tab2Id;
		jQuery.ajax({
			url : encodeURI(url),
			type : 'patch',
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function() {
				$.springLemonDialog({
	   		         content: '现场产品质量信息已保存！',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
			}

		});
	});
	//tab3保存按钮
	jQuery('.saveTab3Btn').on("click", function() {
		
		var tab3Id = $("#tab3Id").val();
		
		var d= 
		{
				"repairCode":$("#repairCode").val(),
				"setDate":$("#setDate").val(),
				"outCode":$("#outCode").val(),
				"seatCode":$("#seatCode").val(),
				"inPressure":$("#inPressure").val(),
				"outPressure":$("#outPressure").val(),
				"lube":$("#lube").val(),
				"caseTemp":$("#caseTemp").val(),
				"bearingTemp":$("#bearingTemp").val(),"pumpValue":$("#pumpValue").val(),
				"balanceValue":$("#balanceValue").val(),"motorCurrent":$("#motorCurrent").val(),
				"inPressure1":$("#inPressure1").val(),"outPressure1":$("#outPressure1").val(),
				"lube1":$("#lube1").val(),"caseTemp1":$("#caseTemp1").val(),
				"bearingTemp1":$("#bearingTemp1").val(),"pumpValue1":$("#pumpValue1").val(),
				"balanceValue1":$("#balanceValue1").val(),"motorCurrent1":$("#motorCurrent1").val(),
				"outputTap":$("#outputTap").val(),"minTap":$("#minTap").val(),
				"inputTap":$("#inputTap").val(),"stopTap":$("#stopTap").val(),
				"minLevelTap":$("#minLevelTap").val(),"abraseState":$("#abraseState").val(),
				"horizontalCenter":$("#horizontalCenter").val(),"verticalCenter":$("#verticalCenter").val(),
				"opening":$("#opening").val(),"axialSize":$("#axialSize").val(),
				"horizontalCenter1":$("#horizontalCenter1").val(),"verticalCenter1":$("#verticalCenter1").val(),
				"opening1":$("#opening1").val(),"axialSize1":$("#axialSize1").val(),
				"questionDealSuggest":$("#questionDealSuggest").val(),"servicePerson":$("#servicePerson").val(),
				"serviceDate":$("#serviceDate").val(),"userAdvise":$("#userAdvise").val(),
				"memo":$("#memo").val()
		}
		var url = window.rootPath + '/api/afterSaleTicketAdvices/' + tab3Id;
		jQuery.ajax({
			url : encodeURI(url),
			type : 'patch',
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function() {
				$.springLemonDialog({
	   		         content: '已保存！',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
			}

		});
	});
	//保存审批信息
	jQuery('.save-submit').on("click", function() {
		var tab3Id = $("#tab3Id").val();
		if (jQuery("#tab3Form") && !jQuery("#tab3Form").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {return;}
		var dutyBlongThree=$('input:radio[name="dutyBlongThree"]:checked').val();
		var outsourcing=$('input:radio[name="outsourcing"]:checked').val();
		var selfMake=$('input:radio[name="selfMake"]:checked').val();
		var d= 
		{
				"regulationCode":$("#regulationCode").val(),"deliveryDate":$("#deliveryDate").val(),
				"outCode":$("#outCode").val(),"sendDate":$("#sendDateThree").val(),
				"dutyBlong":dutyBlongThree,"outsourcing":outsourcing,
				"selfMake":selfMake,"payReason":$("#payReason").val(),
				"summation":$("#summation").val(),"financePerson":$("#financePerson").val(),
				"dealDate":$("#dealDate").val(),"sendWay":$("#sendWay").val(),
				"sendPlace":$("#sendPlace").val(),"receiveUnit":$("#receiveUnit").val(),
				"contactPerson":$("#contactPerson").val(),"contactPhone":$("#contactPhone").val(),
				"postNumber":$("#postNumber").val(),"dealSuperior":$("#dealSuperior").val()
		}
		var url = window.rootPath + '/api/afterSaleTicketAdvices/' + tab3Id;
		jQuery.ajax({
			url : encodeURI(url),
			type : 'patch',
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function() {
				var operateType = "售后服务单办理";
				var content = "保存三包服务单【"+jQuery("#unitName").val()+"】审批信息";
				addOperateLog(operateType,content);
				$.springLemonDialog({
	   		         content: '已保存！',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
			}

		});
	});
	jQuery(".addDeviceBtn").on('click',function(){
		$("#partName").val(""),
		$("#figureNumber").val(""),
	    $("#material").val(""),
		$("#quantity").val(""),
		$("#material").val(""),
		$("#price").val(""),
		$("#sumPrice").val(''),
		$("#deviceMemo").val(""),
		jQuery("#payDeviceModal").modal('show');
	});
	//赔偿零件弹窗保存按钮
	jQuery(".saveDeviceBtn").on('click',function(){
		if (jQuery("#payDeviceForm") && !jQuery("#payDeviceForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		}
		;
		jQuery(".saveDeviceBtn").attr("disabled","disabled");
		var ticketId = jQuery("#ticketId").val();
		var url = window.rootPath + "/ui/afterSaleInHouse/saveDevice";
		var d={
				"partName" : $("#partName").val(),
				"figureNumber" : $("#figureNumber").val(),
				"material" : $("#material").val(),
				"quantity" : $("#quantity").val(),
				"material" : $("#material").val(),
				"price" : $("#price").val(),
				"sumPrice" : $("#sumPrice").val(),
				"deviceMemo" : $("#deviceMemo").val(),
				"afterSaleTicket":{"id":ticketId}
		};
		jQuery.ajax({
			type : 'post',
			url : url,
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
	    		serverTable.draw();
	    		jQuery(".saveDeviceBtn").removeAttr("disabled");
				jQuery('#payDeviceModal').modal('hide');
				
			}
		});
	});
	//删除赔偿设备
	jQuery('.deleteDeviceBtn').on("click",function(){
		var selects=getSelect("#payDeviceTable",1);
    	var url = window.rootPath + "/ui/afterSaleInHouse/delete";
 	   delSelect(selects,url,serverTable)
	});
	//三包零件赔偿表datatable
    var serverData = {};
    serverData.ticketId =$("#ticketId").val();
    var serverTable;
    jQuery(document).ready(function () {
    	
        serverTable = jQuery('#payDeviceTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/afterSaleInHouse/getPayDevicePage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            "iDisplayLength": 5,
            columns: [
                {
                data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate =
                                       '<input type="checkbox" class="checkchild">';
                        return operate;

                }
                },
                {
                    data: 'partName'
                },
                {
                    data: 'figureNumber'
                },
                {
                    data: 'quantity'
                },
                {
                    data: 'material',
                    
                },
                {
                    data: 'price',
                    
                },
                {
                    data: 'sumPrice',
                    
                },
                {
                    data: 'deviceMemo',
                    
                } 
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

            }
        });
    	});
    ///////////////////////赔偿零件弹窗数据自动计算///////////////////
    jQuery('#quantity').blur(function(){
    	if($("#quantity").val() != '' && $("#price").val() != ''){
    		var price = $("#price").val();
    		var quantity = $("#quantity").val();
    		if(!isNaN(Number(price)) && !isNaN(Number(quantity))){
    			$("#sumPrice").val(price*quantity);
    		}
    	}
    });
    jQuery('#price').blur(function(){
    	if($("#quantity").val() != '' && $("#price").val() != ''){
    		var price = $("#price").val();
    		var quantity = $("#quantity").val();
    		if(!isNaN(Number(price)) && !isNaN(Number(quantity))){
    			$("#sumPrice").val(price*quantity);
    		}
    	}
    });
	///////////////////////附件上传下载///////////////////////////
	//传真，邮件
	jQuery(".upMail").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"传真邮件",null,'mailNum');
	});
	jQuery(".downMail").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'传真邮件',1);
	});
    showANum(jQuery("#ticketId").val(),'传真邮件',"mailNum");
	
	//质量反馈表
	jQuery(".upQuality").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"质量反馈表",null,'qualityNum');
	});
	jQuery(".downQuality").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'质量反馈表',1);
	});
    showANum(jQuery("#ticketId").val(),'质量反馈表',"qualityNum");
	//技术方案
	jQuery(".upPlan").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"技术方案",null,'planNum');
	});
	jQuery(".downPlan").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'技术方案',1);
	});
	showANum(jQuery("#ticketId").val(),'技术方案',"planNum");
	//返厂泵处理表
	jQuery(".upDealForm").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"返厂泵处理表",null,'dealFormNum');
	});
	jQuery(".downDealForm").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'返厂泵处理表',1);
	});
	showANum(jQuery("#ticketId").val(),'返厂泵处理表',"dealFormNum");
	//其它
	jQuery(".upOther").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"其它",null,'otherNum');
	});
	jQuery(".downOther").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'其它',1);
	});
	showANum(jQuery("#ticketId").val(),'其它',"otherNum");
	//三包办理明细
	jQuery(".upDetail").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"三包办理明细",null,"detailNum");
	});
	jQuery(".downDetail").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'三包办理明细',1);
	});
	showANum(jQuery("#ticketId").val(),'三包办理明细',"detailNum");
	//执行过程
	jQuery(".upProcess").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"执行过程",null,"processNum");
	});
	jQuery(".downProcess").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'执行过程',1);
	});
	showANum(jQuery("#ticketId").val(),'执行过程',"processNum");
	//三包图片
	jQuery(".upPicture").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"三包图片",null,'pictureNum');
	});
	jQuery(".downPicture").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'三包图片',1);
	});
	showANum(jQuery("#ticketId").val(),'三包图片',"pictureNum");
	//返厂维修意见
	jQuery(".backAdviceUp").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"返厂维修意见",null,"backAdviceNum");
	});
	jQuery(".backAdviceDown").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'返厂维修意见',1);
	});
	showANum(jQuery("#ticketId").val(),'返厂维修意见',"backAdviceNum");
	
	///下载回执单
	jQuery(".downRecipt").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'售后回执单',0);
	});
	showANum(jQuery("#ticketId").val(),'售后回执单',"receiptNum");
	   ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});
});